//
//  LLGPictureView.swift
//  Weibo30LLG
//
//  Created by 李龙 on 16/8/13.
//  Copyright © 2016年 lilong. All rights reserved.
//

import UIKit
import SDWebImage

//cell标记
private let PictureViewCellIdentifier = "PictureViewCellIdentifier"

//间距
private let itemMargin: CGFloat = 5
//宽度
private let itemWidth = (ScreenWidth - 2 * StatuseTableViewCellMargin - 2 * itemMargin) / 3

class LLGPictureView: UICollectionView {
    
    var picUrls: [LLGStatusePictureInfo]? {
    
        didSet {
            
            messageLabel.text = "\(picUrls?.count ?? 0)"
            
            let size = calcSizeWithCount(picUrls?.count ?? 0)
            
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(size)
            }
            
            self.reloadData()
            
        }
    }

    //显示配图个数
    private lazy var messageLabel = UILabel(fontSize: 20, color: UIColor.whiteColor())
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        //size
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
        
        //水平间距
        flowLayout.minimumInteritemSpacing = itemMargin
        //垂直间距
        flowLayout.minimumLineSpacing = itemMargin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: --添加控件,设置约束
    private func setupUI() {
        
        self.registerClass(LLGPictureViewCell.self, forCellWithReuseIdentifier: PictureViewCellIdentifier)
        
        dataSource = self
    
        addSubview(messageLabel)
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
    }
    
    // MARK: --通过配图个数计算配图大小
    private func calcSizeWithCount(count: Int) -> CGSize {
    
        //列数
        let cols = count > 3 ? 3 : count
        //行数
        let rows = (count - 1) / 3 + 1
        
        //宽度
        let currentWidth = itemWidth * CGFloat(cols) + CGFloat(cols - 1) * itemMargin
        //高度
        let currentHeight = itemWidth * CGFloat(rows) + CGFloat(rows - 1) * itemMargin
        
        return CGSize(width: currentWidth, height: currentHeight)
    }
}

// MARK: --数据源方法
extension LLGPictureView: UICollectionViewDataSource {

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return picUrls?.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(PictureViewCellIdentifier, forIndexPath: indexPath) as! LLGPictureViewCell

        //设置数据
        cell.picInfo = picUrls![indexPath.item]
        
        return cell
    }
}

//自定义cell
class LLGPictureViewCell: UICollectionViewCell {

    var picInfo: LLGStatusePictureInfo? {
    
        didSet {
        
            if let url = picInfo?.thumbnail_pic {
                imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
                
                //设置是否显示gif标示
                gifImageView.hidden = !url.hasSuffix(".gif")
            }
 
        }
    }
    
    //gif图
    private lazy var gifImageView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "timeline_image_gif"))
        
        return view
    }()
    
    //图片框
    private lazy var imageView: UIImageView = {
    
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        //修改显示模型为等比填充,保证图片比例不变
        view.contentMode = UIViewContentMode.ScaleAspectFill
        //切除多余
        view.clipsToBounds = true
        
        return view
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: --添加控件,设置约束
    private func setupUI() {
    
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
    }
    
}
